import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

public class MyUtil {

    // 指令对应的选项个数map以及指令对应的选项格式map
    public static HashMap<String, Integer[]> command_len = new HashMap<String, Integer[]>();
    public static HashMap<String, String[]> command_option = new HashMap<String, String[]>();
    public static HashMap<String, String[]> person_option = new HashMap<String, String[]>();

    // 输出
    public static final String ERROR_COUNT = "Params' count illegal";
    public static final String ERROR_FORM = "Input illegal";
    public static final String BYE = "----- Good Bye! -----";
    public static final String NO_COMMAND = "Command not exist";
    public static final String HAVE_DISH = "Dish exists";
    public static final String NO_DISH = "Dish does not exist";
    public static final String CHANG_DISH_ERROR = "Change dish illegal";
    public static final String EMPTY = "Empty Menu";
    public static final String ERROR_ID = "Did input illegal";
    // inner
    public static final String ERROR_PAGE = "Page slice method's params input illegal";
    public static final String INNER_QUIT = "Exit page check mode";
    public static final String INNER_NO_COMMAND = "Call inner method illegal";
    public static final String INNER_PAGE_EMPTY = "Menu is empty, exit page check mode";
    public static final String INNER_FIRST_PAGE = "This is the first page";
    public static final String INNER_LAST_PAGE = "This is the last page";
    public static final String INNER_HELP = "n-next page,l-last page,f-first page,q-quit";
    // sudo
    public static final String SUDO_IN = "Enter sudo mode";
    public static final String SUDO_NO_COMMAND = "Call sudo method illegal";
    public static final String SUDO_QUIT = "Quit sudo mode";
    public static final String SUDO_PID_ERROR = "PID illegal";
    public static final String SUDO_PID_EXISTS = "PID exists";
    public static final String SUDO_CUSTOMER = "Customer";
    public static final String SUDO_WAITER = "Waiter";
    public static final String SUDO_COOK = "Cook";
    public static final String SUDO_EMPTY = "Empty person list";
    public static final String SUDO_PWD_UNMATCHED = "Password not match";
    public static final String SUDO_LOGIN_SUCCESS = "Login success";
    // login
    public static final String LOGIN_LOGOUT = "Logout success";
    public static final String CUSTOMER_RECHARGE_ERROR = "Recharge input illegal";
    // 通用scanner
    public static Scanner in = new Scanner(System.in);

    // 初始化指令与数量选项对应关系
    public static void init() {
        // 添加指令及其可能的参数数量
        MyUtil.command_len.put("np", new Integer[] { 4 });
        MyUtil.command_len.put("QUIT", new Integer[] { 1 });
        MyUtil.command_len.put("nd", new Integer[] { 5 });
        MyUtil.command_len.put("udd", new Integer[] { 4 });
        MyUtil.command_len.put("gd", new Integer[] { 3, 5 });
        MyUtil.command_len.put("pm", new Integer[] { 1, 3 });
        MyUtil.command_len.put("QUIT", new Integer[] { 1 });
        MyUtil.command_len.put("SUDO", new Integer[] { 1 });
        MyUtil.command_len.put("login", new Integer[] { 4 });
        MyUtil.command_len.put("sncu", new Integer[] { 5 });
        MyUtil.command_len.put("snwa", new Integer[] { 5 });
        MyUtil.command_len.put("snco", new Integer[] { 5 });
        MyUtil.command_len.put("dcu", new Integer[] { 2 });
        MyUtil.command_len.put("dwa", new Integer[] { 2 });
        MyUtil.command_len.put("dco", new Integer[] { 2 });
        MyUtil.command_len.put("pp", new Integer[] { 1 });
        MyUtil.command_len.put("SQ", new Integer[] { 1 });
        MyUtil.command_len.put("chgpw", new Integer[] { 3 });
        MyUtil.command_len.put("myinfo", new Integer[] { 1 });
        MyUtil.command_len.put("back", new Integer[] { 1 });
        MyUtil.command_len.put("rc", new Integer[] { 2 });
        MyUtil.command_len.put("gb", new Integer[] { 1 });
        MyUtil.command_len.put("aplVIP", new Integer[] { 1 });

        // 添加指令及其选项
        MyUtil.command_option.put("gd", new String[] { "-id", "-key" });
        MyUtil.command_option.put("udd", new String[] { "-n", "-t", "-p" });
        MyUtil.command_option.put("login", new String[] { "-n", "-i" });

        //
        MyUtil.person_option.put(SUDO_CUSTOMER, new String[] { "rc", "gb", "aplVIP" });
        MyUtil.person_option.put(SUDO_WAITER, new String[] {});
        MyUtil.person_option.put(SUDO_COOK, new String[] {});

    }

    // 输入当前行，判断参数数量是否合理，合理则返回true否则返回false
    public static boolean checkCommandLen(String[] command) {
        for (Integer len : command_len.get(command[0])) {
            if (command.length == len) {
                return true;
            }
        }
        return false;
    }

    // 输入当前行和该指令的选项位置，判断选项格式是否合理，合理则返回true否则返回false
    public static boolean checkCommandOption(String[] command, int place) {
        if (place == 0) {
            return true;
        }
        if (command.length > place) {
            for (String op : command_option.get(command[0])) {
                if (command[place].equals(op)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean checkMainCommand(String[] command, int place) {
        if (!MyUtil.checkCommandOption(command, place)) {
            System.out.println(MyUtil.NO_COMMAND);
            return false;
        } else if (!MyUtil.checkCommandLen(command)) {
            System.out.println(MyUtil.ERROR_COUNT);
            return false;
        } else {
            return true;
        }
    }

    public static boolean checkSudoCommand(String[] command, int place) {
        if (!MyUtil.checkCommandOption(command, place)) {
            System.out.println(MyUtil.SUDO_NO_COMMAND);
            return false;
        } else if (!MyUtil.checkCommandLen(command)) {
            System.out.println(MyUtil.ERROR_COUNT);
            return false;
        } else {
            return true;
        }
    }

    public static boolean checkLoginCommand(Person person, String[] command, int place) {
        for (String op : person_option.get(person.getClass().getName())) {
            if (op.equals(command[0])) {
                return checkMainCommand(command, place);
            }
        }
        System.out.println(MyUtil.NO_COMMAND);
        return false;
    }

    // 检查字符串是否为int
    public static boolean checkInt(String str) {
        String regex = "^-?\\d+";
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(str).matches();
    }

    // 检查字符串是否为非负整数
    public static boolean checkNoNegativeInt(String str) {
        String regex = "^\\d+";
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(str).matches();
    }

    // 检查字符串是否为非负浮点数
    public static boolean checkNoNegativeDouble(String str) {
        String regex = "^\\d+(\\.\\d+)?";
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(str).matches();
    }

    // 检查字符串str是否包含key字串，不区分大小写
    public static boolean containKey(String str, String key) {
        StringBuffer tmp = new StringBuffer();
        for (int i = 0; i + key.length() <= str.length(); i++) {
            tmp.append(str.substring(i, i + key.length()));
            if (tmp.toString().compareToIgnoreCase(key) == 0) {
                return true;
            }

            tmp.setLength(0);
        }
        return false;
    }

    // 输入已排序的DishList及输出范围，进行输出
    public static void printComparedDishList(List<Dish> dishList, int left, int right) {
        for (int i = left; i < right; i++) {
            System.out.println((i + 1) + ". " + dishList.get(i).toString());
        }
    }

    public static void printComparedPersonList(List<Person> personList, int left, int right) {
        for (int i = left; i < right; i++) {
            System.out.println((i + 1) + "." + personList.get(i).toString());
        }
    }

    // 输入DishList，当前页码，最终页剩余菜品个数，总页数，每页菜品个数。按页码进行输出
    public static void printPageDishList(List<Dish> dishList, int now_page, int last_page, int tot_page, int page_num) {
        int left = (now_page - 1) * page_num;
        int right = left + (now_page == tot_page ? last_page : page_num);

        System.out.println("Page: " + now_page);
        for (int i = left; i < right; i++) {
            System.out.println((i - left + 1) + ". " + dishList.get(i).toString());
        }
        System.out.println(MyUtil.INNER_HELP);
    }

    // 在map中寻找菜名包括key的dish，返回所有包含key的Dish的List
    public static List<Dish> getListByKey(HashMap<String, Dish> idmenu, String key) {
        List<Dish> found = new ArrayList<Dish>();
        for (String DishID : idmenu.keySet()) {
            if (MyUtil.containKey(idmenu.get(DishID).getName(), key)) {
                found.add(idmenu.get(DishID));
            }
        }
        return found;
    }

    public static Person containName(HashMap<String, Person> personMap, String name) {
        for (String PID : personMap.keySet()) {
            if (personMap.get(PID).getName().equals(name)) {
                return personMap.get(PID);
            }
        }
        return null;
    }

    public static Person containPhone(HashMap<String, Person> personMap, String phone) {
        for (String PID : personMap.keySet()) {
            if (personMap.get(PID).getPhoneNum().equals(phone)) {
                return personMap.get(PID);
            }
        }
        return null;
    }

    // 判断新菜品名字是否重复
    public static boolean checkRepeatName(HashMap<String, Dish> idmenu, String name) {
        for (String id : idmenu.keySet()) {
            if (idmenu.get(id).getName().equals(name)) {
                return false;
            }
        }
        return true;
    }

}
